package com.mobi.Survey.lib.db;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mobi.Survey.lib.model.FavoritePojo;
import com.mobi.Survey.lib.model.Pojo;

public class FavoriteTable extends AbstractTable {

	public static final String TABLE = "favorite_surveys";

	public static final String COL_ID = "_id";
	public static final String COL_USER_ID = "user_id";
	public static final String COL_IMAGE_URL = "image_url";
	public static final String COL_SURVEY_ID = "survey_id";
	public static final String COL_DESCRIPTION = "form_description";

	public FavoriteTable(Context ctx) {
		super(ctx);
	}

	public FavoriteTable() {

	}

	@Override
	public long insertRow(Pojo obj) {

		FavoritePojo favorite = (FavoritePojo) obj;
		SQLiteDatabase db = dbHelper.getWritableDatabase();

		ContentValues cv = new ContentValues();
		cv.put(COL_USER_ID, favorite.getUserId());
		cv.put(COL_IMAGE_URL, favorite.getIconUrl());
		cv.put(COL_SURVEY_ID, favorite.getSurveyId());
		cv.put(COL_DESCRIPTION, favorite.getDescription());

		long id = db.insert(TABLE, null, cv);
		return id;

	}

	@Override
	public boolean deleteRow(long id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int effected = db.delete(TABLE, COL_SURVEY_ID + " = ? ", new String[] { id + "" });
		return effected > 0;
	}
	
	public Set<Long> getAllFavoriteSurveyIds() {
		Set<Long> surveyIds = new HashSet<Long>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(TABLE, new String[] {  COL_SURVEY_ID }, null, null, null, null, null);

		if (cursor != null) {
			if (cursor.moveToFirst() && cursor.getCount() > 0) {

				do {

					

					surveyIds.add(cursor.getLong(0));
			 
				
				} while (cursor.moveToNext());

			}
			cursor.close();
		}

		return surveyIds;

	}

	public List<FavoritePojo> getAllFavorites() {
		List<FavoritePojo> favorites = new ArrayList<FavoritePojo>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(TABLE, new String[] { COL_ID, COL_USER_ID, COL_IMAGE_URL, COL_SURVEY_ID,
				COL_DESCRIPTION }, null, null, null, null, null);

		if (cursor != null) {
			if (cursor.moveToFirst() && cursor.getCount() > 0) {

				do {

					FavoritePojo favorite = new FavoritePojo();

					favorite.setId(cursor.getLong(0));
					favorite.setUserId(cursor.getString(1));
					favorite.setIconUrl(cursor.getString(2));
					favorite.setSurveyId(cursor.getLong(3));
					favorite.setDescription(cursor.getString(4));
					favorite.setFavorite(true);
					favorites.add(favorite);
				
				} while (cursor.moveToNext());

			}
			cursor.close();
		}

		return favorites;

	}

	@Override
	public boolean createTable(SQLiteDatabase sqliteDb) {
		String query = "create table " + TABLE + "( " + COL_ID + " integer primary key autoincrement, " + COL_USER_ID
				+ " text, " + COL_IMAGE_URL + " text, " + COL_DESCRIPTION + " text, " + COL_SURVEY_ID + " integer )";
		sqliteDb.execSQL(query);
		printMessage(query);
		return true;
	}

	@Override
	public void deleteAllData() {
		// TODO Auto-generated method stub

	}

}
